﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;

namespace SchoolManagementSystem
{
    public partial class Karyawan : Form
    {
        private DataTable dTable = null;
        private BindingSource bindingSource = new BindingSource();
        private KaryawanEdit edit = null;
        private KaryawanAdd add = null;

        public Karyawan()
        {
            InitializeComponent();
            Style.setDatagridviewStyle(karyawanDataGridView);
        }

        private void Karyawan_Load(object sender, EventArgs e)
        {
            this.refreshTableData();
        }

        private void refreshTableData()
        {
            this.karyawanDataGridView.Columns.Clear();

            dTable = Database.getInstance().query("select * from Employee");

            bindingSource.DataSource = dTable;
            this.karyawanDataGridView.DataSource = bindingSource;


            // Add Edit Column
            DataGridViewLinkColumn editColumn = new DataGridViewLinkColumn();
            editColumn.UseColumnTextForLinkValue = true;
            editColumn.Name = "edit";
            editColumn.Text = "Edit";
            editColumn.HeaderText = "Edit";
            editColumn.ReadOnly = true;
            editColumn.VisitedLinkColor = Color.Blue;
            this.karyawanDataGridView.Columns.Add(editColumn);
            this.karyawanDataGridView.Columns["edit"].Width = 50;
            this.karyawanDataGridView.Columns["edit"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // Add Delete Column
            DataGridViewLinkColumn deleteColumn = new DataGridViewLinkColumn();
            deleteColumn.UseColumnTextForLinkValue = true;
            deleteColumn.Name = "delete";
            deleteColumn.HeaderText = "Delete";
            deleteColumn.Text = "Delete";
            deleteColumn.ReadOnly = true;
            deleteColumn.VisitedLinkColor = Color.Blue;
            this.karyawanDataGridView.Columns.Add(deleteColumn);
            this.karyawanDataGridView.Columns["delete"].Width = 50;
            
            // Modify table
            this.karyawanDataGridView.Columns["emp_id"].HeaderText = "NIK";
            this.karyawanDataGridView.Columns["emp_id"].Width = 50;
            this.karyawanDataGridView.Columns["emp_name"].HeaderText = "Nama Karyawan";
            this.karyawanDataGridView.Columns["emp_gender"].HeaderText = "Jenis Kelamin";
            this.karyawanDataGridView.Columns["emp_gender"].Width = 50;
            this.karyawanDataGridView.Columns["emp_gender"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.karyawanDataGridView.Columns["emp_birthdate"].HeaderText = "Tanggal Lahir";
            this.karyawanDataGridView.Columns["emp_birthplace"].HeaderText = "Tempat Lahir";
            this.karyawanDataGridView.Columns["emp_address"].HeaderText = "Alamat";
            this.karyawanDataGridView.Columns["emp_role"].HeaderText = "Jabatan";
            this.karyawanDataGridView.Columns["emp_edu"].HeaderText = "Pendidikan Terakhir";
            this.karyawanDataGridView.Columns["emp_start"].HeaderText = "Mulai Tugas";
            this.karyawanDataGridView.Columns["emp_duration"].HeaderText = "Masa Kerja";
            this.karyawanDataGridView.Columns["emp_duration"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.karyawanDataGridView.Columns["emp_yayasan"].HeaderText = "SK Yayasan";
            this.karyawanDataGridView.Columns["emp_phone"].HeaderText = "Telepon";
            this.karyawanDataGridView.Columns["emp_mobile"].HeaderText = "HP";
            this.karyawanDataGridView.Columns["emp_email"].HeaderText = "Email";

            this.karyawanDataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            this.karyawanDataGridView.Refresh();
        }

        private void karyawanDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.karyawanDataGridView.Columns[e.ColumnIndex].Name == "edit")
            {
                if (this.edit == null || this.edit.IsDisposed)
                    edit = new KaryawanEdit(this, this.dTable, e.RowIndex,
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_name"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_gender"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_birthdate"].Value,
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_birthplace"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_address"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_role"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_edu"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_start"].Value,
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_duration"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_yayasan"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_phone"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_mobile"].Value.ToString(),
                        this.karyawanDataGridView.Rows[e.RowIndex].Cells["emp_email"].Value.ToString());

                edit.MdiParent = this.MdiParent;
                edit.Show();
                edit.Activate(); 
            }
            else if (this.karyawanDataGridView.Columns[e.ColumnIndex].Name == "delete")
            {
                var result = MessageBox.Show("Apakah Anda ingin menghapus orangtua tersebut ?", "Peringatan !", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    this.karyawanDataGridView.Rows.RemoveAt(e.RowIndex);
                    this.dTable.Rows[e.RowIndex].Delete();      
                    Database.getInstance().updateDB(this.dTable);
                    this.karyawanDataGridView.Refresh(); 
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // this.refreshTableData();

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.add == null || this.add.IsDisposed)
                add = new KaryawanAdd();

            add.Show();
            add.Activate();
        }

        

    }
}
